var UI = {}

UI.Form = function() {
}

UI.Form.prototype.alert = function(msg) {
	alert(msg);
}

UI.ButtonControl = function(form, container, defn) {
	var ctl = this
	var btn = document.createElement("BUTTON")
	btn.appendChild(document.createTextNode(defn.caption))
	btn.style.position = 'absolute'
	btn.style.left = defn.left + 'px'
	btn.style.top = defn.top + 'px'
	btn.style.width = defn.width + 'px'
//	btn.style.height = defn.height + 'px'
	document.body.appendChild(btn)
	
	btn.onclick = function() {
		if (defn.Click)
			defn.Click.call(ctl.form)
	}
}

UI.LabelControl = function(form, container, defn) {
	var btn = document.createElement("DIV")
	btn.appendChild(document.createTextNode(defn.text))
	btn.style.position = 'absolute'
	btn.style.left = defn.left + 'px'
	btn.style.top = defn.top + 'px'
	btn.style.width = defn.width + 'px'
	btn.style.height = defn.height + 'px'
	document.body.appendChild(btn)
}

UI.TextControl = function(form, container, defn) {
	var btn = document.createElement("INPUT")
	btn.type = "text";
	btn.style.position = 'absolute'
	btn.style.left = defn.left + 'px'
	btn.style.top = defn.top + 'px'
	btn.style.width = defn.width + 'px'
	btn.style.height = defn.height + 'px'
	this._dom = btn
	document.body.appendChild(btn)
}

UI.CheckBox = function(form, container, defn) {
	var el = document.createElement("DIV")
	var btn = document.createElement("INPUT")
	var lbl = document.createElement("SPAN")
	lbl.style.cursor = "default"
	btn.type = "checkbox";
	el.style.position = 'absolute'
	el.style.left = defn.left + 'px'
	el.style.top = defn.top + 'px'
	el.style.width = defn.width + 'px'
	el.style.height = defn.height + 'px'
	this._dom = btn
	
	lbl.appendChild(document.createTextNode(defn.caption))
	el.appendChild(btn)
	el.appendChild(lbl)
	
	lbl.onmousedown = function(e) {
		btn.checked = !btn.checked
		if (e.stopPropagation)
			e.stopPropagation()
			else
				e.cancelBubble = true
				return false
				}
	
	document.body.appendChild(el)
}

UI.TextControl.prototype.getText = function() {
	return this._dom.value
}

UI.TextControl.prototype.setText = function(v) {
	this._dom.value = v
}

UI.CheckBox.prototype.getChecked = function() {
	return this._dom.value
}

UI.CheckBox.prototype.setChecked = function(v) {
	this._dom.value = v
}

UI._controlFactories = {
	button: UI.ButtonControl,
	label: UI.LabelControl,
	textfield: UI.TextControl,
	checkbox: UI.CheckBox,

}

UI.Form.prototype._loadControls = function(defn) {
	var ctls = defn.controls
	this.allControls = []
	if (ctls != null) {
		for (var idx = 0; idx < ctls.length; idx++) {
			var ctl = ctls[idx];
			var factory = UI._controlFactories[ctl.type];
			if (factory != null) {
				var instance = new factory(this, this, ctl)
				this[ctl.name] = instance
				this.allControls.push(instance)
				instance.form = this
			}
		}
	}
}

UI._startupForms = []

UI.DefineWindow = function(defn) {
	var constructor = function() {
		this._loadControls(defn)
		if (defn.Load)
			defn.Load.call(this)
	}
	constructor.prototype = new UI.Form()
	if (defn.startup)
		UI._startupForms.push(constructor)
	return constructor;
}

window.onload = function() {
	for (var idx = 0; idx < UI._startupForms.length; idx++) {
		var fm = UI._startupForms[idx]
		new fm()
	}
}